package Modelo.Controler;

import Model.Beans.Answer;
import Model.Beans.Question;
import Model.Beans.User;
import Model.DAO.UserDAO;
import Model.Servicios.AnswerSrv;
import Model.Servicios.QuestionSrv;
import Model.Servicios.UserSrv;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.struts2.interceptor.SessionAware;


/**
 *
 * @author Pashi
 */
public class ForumMgmtAction extends ActionSupport 
//        implements SessionAware
{
    private List<Question> questionsBean=new ArrayList<Question>();;
    private List<Answer> answersBean=new ArrayList<Answer>();
    private Question questionBean;
    private Answer answerBean;    
    private String idQ;   
    private String idA;    
    private User user;

    private Map<String, Object> mapSession;
    
    public String showAll()throws Exception {
        QuestionSrv questBD = new QuestionSrv();
        questionsBean= questBD.getQuestions();
        return SUCCESS;
    }

    public String ask()throws Exception {
        return "ask";
    }
    
    public String answer()throws Exception {
        return "answer";
    }

    public String answerPublish()throws Exception {
           if(getAnswerBean()==null||getAnswerBean().getAnswer().compareTo("")==0){              
               return ERROR;
           }else{       
               user=getUser();
               answerBean.setDate(new Date());
               questionBean=new QuestionSrv().getQuestion(idQ);
               answerBean.setQuestion(questionBean);
               answerBean.setUser(user);

               AnswerSrv answBD = new AnswerSrv();
               answBD.insertAnswer(answerBean);

              // answersBean.add(answerBean);
               return showAll();
           }
      }
     
    public String askPublish()throws Exception {
           if(getQuestionBean()==null||getQuestionBean().getQuestion().compareTo("")==0){              
               return ERROR;
           }else{
               user=getUser();
               questionBean.setDate(new Date());
               questionBean.setUser(user);

               QuestionSrv questBD = new QuestionSrv();
               questBD.insertQuestion(questionBean);
             //  questionsBean.add(questionBean);
               return showAll();
           }
            
        }
    
    public String deleteQ()throws Exception {
        new QuestionSrv().deleteById(getIdQ());
        return showAll();
    }
    public String deleteA()throws Exception {
        new AnswerSrv().deleteById(getIdA());        
        return showAll();
    }

    public Answer getAnswerBean() {
        return answerBean;
    }

    public void setAnswerBean(Answer answerBean) {
        this.answerBean = answerBean;
    }

    public Question getQuestionBean() {
        return questionBean;
    }

    public void setQuestionBean(Question questionBean) {
        this.questionBean = questionBean;
    }

    public List<Answer> getAnswersBean() {
        return answersBean;
    }

    public void setAnswersBean(List<Answer> answersBean) {
        this.answersBean = answersBean;
    }

    public List<Question> getQuestionsBean() {
        return questionsBean;
    }

    public void setQuestionsBean(List<Question> questionsBean) {
        this.questionsBean = questionsBean;
    }

    public String getIdA() {
        return idA;
    }

    public void setIdA(String idA) {
        this.idA = idA;
    }

    public String getIdQ() {
        return idQ;
    }

    public void setIdQ(String idQ) {
        this.idQ = idQ;
    }

    public User getUser() {
        UserDAO uSrv=new UserDAO();
        user=uSrv.searchUserByStudent(1);
        return user;
    }


    public void setUsuario(User usuario) {
         this.user = usuario;
    }

    public void setSession(Map<String, Object> map) {
        mapSession=map;
    }
    
}
